package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseLListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    private PurchaseLListDao purchaseLListDao;

    @Resource
    private UserDao userDao;

    @Resource
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Resource
    private GoodsDao goodsDao;

    @Override
    public void savePurchase(PurchaseList purchaseList, PurchaseListGoods purchaseListGoodsStr) {

        //将数据添加到purchaseList中
        PurchaseList purchaseList1 = new PurchaseList();
        purchaseList1.setState(1);
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        purchaseList1.setUserId(currentUser.getUserId());
        BeanUtils.copyProperties(purchaseList, purchaseList1);
        purchaseLListDao.insertPurchaseList(purchaseList1);

        //添加PurchaseListGoods
        PurchaseListGoods goods = new PurchaseListGoods();


        //查询PurchaseList的id
        goods.setPurchaseListId(purchaseList1.getPurchaseListId());


            BeanUtils.copyProperties(purchaseListGoodsStr, goods);
            purchaseListGoodsDao.insertPurchaseListGoods(goods);
        Goods goods1 = goodsDao.selectGoods(purchaseListGoodsStr.getGoodsId());
        goods1.setInventoryQuantity(purchaseListGoodsStr.getGoodsNum()+goods1.getInventoryQuantity());
    }
}
